package com.hyc.myvideo.ui.fragment.home;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import butterknife.BindView;
import com.acmenxd.toaster.Toaster;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.adapter.TrainingCatalogAdapter;
import com.hyc.myvideo.data.api.model.BaseResult;
import com.hyc.myvideo.data.api.model.Practice;
import com.hyc.myvideo.data.api.model.PracticeModel;
import com.hyc.myvideo.ui.activity.home.CourseListActivity;
import com.hyc.myvideo.ui.base.BaseFragment;
import java.util.ArrayList;
import java.util.List;
import timber.log.Timber;

/**
 * 修炼目录
 * Created by dfeads on 2017/7/13.
 */

public class TrainingCatalogFragment extends BaseFragment {

  @BindView(R.id.fragment_teacher_pull_refresh) PullToRefreshListView mPullRefresh;
  private TrainingCatalogAdapter trainingCatalogAdapter;
  private List<Practice> practiceList;

  @Override protected int getLayoutId() {
    return R.layout.fragment_teacher;
  }

  @Override protected void initView() {

    practiceList = new ArrayList<>();
    trainingCatalogAdapter = new TrainingCatalogAdapter(getContext(), practiceList);
    mPullRefresh.setAdapter(trainingCatalogAdapter);

    mPullRefresh.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
    mPullRefresh.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
      @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

      }

      @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

      }
    });

    mPullRefresh.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        Timber.e("=======onItemClick======" + position);
        Practice practice = practiceList.get(position - 1);
        if (practice != null) {
          startActivity(new Intent(getContext(), CourseListActivity.class).putExtra("id",
              practice.getCatelog_id()).putExtra("title", practice.getCatelog_name()));
        }
      }
    });
  }

  @Override protected void initData() {
    getPracticeList();
  }

  public void getPracticeList() {
    api.getPracticeList(userInfo.getUser_id());
  }

  @Override protected void onNexts(String result, String method) {
    BaseResult<PracticeModel> practiceModel =
        JSONObject.parseObject(result, new TypeReference<BaseResult<PracticeModel>>() {
        });
    if (practiceModel.isCode()) {
      practiceList.clear();
      practiceList.addAll(practiceModel.getResult().getList());
      trainingCatalogAdapter.updateListView(practiceList);
    } else {
      Toaster.show(practiceModel.getMsg());
    }

    if (mPullRefresh.isRefreshing()) {
      mPullRefresh.onRefreshComplete();
    }
  }

  @Override protected void onError() {
    if (mPullRefresh.isRefreshing()) {
      mPullRefresh.onRefreshComplete();
    }
  }
}
